﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SiteManager;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;

public partial class includes_UcStandByDentist : System.Web.UI.UserControl
{
    #region creating an instance of manager class and Local Variable
    Manager mgr = new Manager(true, "ConnectionString");
    string _fromPage = "";
    bool _isBlank = false;
    bool _isSpecialty = false;
    #endregion

    #region properties



    public bool isSpecialty
    {
        get { return _isSpecialty; }
        set { _isSpecialty = value; }
    }
    public string fromPage
    {
        set { _fromPage = value; }
        get { return _fromPage; }
    }
    public bool isBlank
    {
        get { return _isBlank; }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        //   Response.Write("-----" + this.fromPage);
        btnSubmit.Attributes.Add("onclick", "return " + this.ClientID + "chkError();");
        if (!Page.IsPostBack)
        {
            // fill drop downs
            mgr.BindList("select Id,Specialty from tbl_DentistSpecialty order by Specialty", "Specialty", "Id", ddlSpeciality, "select");
            // mgr.BindList("select stateid,name from tbl_states order by name", "Name", "StateId", ddlState, "");
            mgr.BindList("select [countryname],countrycode from tbl_country where(countrycode='US')OR (countrycode='CA') order by [countryname]", "countryname", "countrycode", ddlCountry, "select");
            //ddlState.Items.Insert(0, new ListItem("--Any--", "0"));

        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string qstring = "";

        if (ddlSpeciality.Items.Count > 1)
        {
            if (ddlSpeciality.SelectedIndex > 0)
            {
                Session["Speciality"] = ddlSpeciality.SelectedItem.Text.Trim().Replace(" ", "_");
                //qstring = "?specialty=" + Session["Speciality"];
                qstring = Session["Speciality"].ToString();
            }
        }

        if (ddlState.Items.Count > 1)
        {
            if (ddlState.SelectedIndex > 0)
            {
                if (qstring == "")
                {
            
                    qstring = ddlState.SelectedItem.Text.Trim().Replace(" ", "_");
                }
                else
                {
                   
                    qstring = qstring + "/" + ddlState.SelectedItem.Text.Trim().Replace(" ", "_");
                }
            }
        }

        if (txtCity.Text.Trim() != "")
        {
            if (qstring == "")
            {
                
                qstring = txtCity.Text.Trim().Replace(" ", "_");
            }
            else
            {
                
                qstring = qstring + "/" + txtCity.Text.Trim().Replace(" ", "_");
            }
        }

        if (txtZipCode.Text.Trim() != "")
        {
            if (qstring == "")
            {
               
                qstring = txtZipCode.Text.Trim().Replace(" ", "_");
            }
            else
            {
                
                qstring = qstring + "/" + txtZipCode.Text.Trim().Replace(" ", "_");
            }
        }

        if (txtLName.Text.Trim().Equals("Last Name:"))
            txtLName.Text = "";



        #region calling stored procedure
        // get the results of the search
        try
        {
            SqlParameter[] sqlParam = new SqlParameter[9];
            sqlParam[8] = new SqlParameter("@CountryId", SqlDbType.VarChar, 2);
            sqlParam[0] = new SqlParameter("@state", SqlDbType.VarChar, 2);
            sqlParam[1] = new SqlParameter("@city", SqlDbType.VarChar, 50);
            sqlParam[2] = new SqlParameter("@zip", SqlDbType.VarChar, 10);
            sqlParam[3] = new SqlParameter("@LName", SqlDbType.VarChar, 50);
            sqlParam[4] = new SqlParameter("@FName", SqlDbType.VarChar, 50);
            sqlParam[5] = new SqlParameter("@spec_id", SqlDbType.Int);
            sqlParam[6] = new SqlParameter("@hospital", SqlDbType.VarChar, 200);
            sqlParam[7] = new SqlParameter("@insurance", SqlDbType.VarChar, 200);

            //sqlParam[4] = new SqlParameter("@rating", SqlDbType.Int);
            //sqlParam[7] = new SqlParameter("@subspec_id", SqlDbType.Int);
            sqlParam[8].Value = ddlCountry.SelectedItem.Value.Equals("0") ? "" : ddlCountry.SelectedItem.Value;
            sqlParam[0].Value = ddlState.SelectedItem.Value.Equals("0") ? "" : ddlState.SelectedItem.Value;
            sqlParam[1].Value = txtCity.Text.Trim();
            sqlParam[2].Value = txtZipCode.Text.Trim();
            sqlParam[3].Value = txtLName.Text.Trim();
            //sqlParam[4].Value = txtFName.Text.Trim();
            sqlParam[5].Value = ddlSpeciality.SelectedItem.Value;

            if (ddlhospital.Visible == true)
                sqlParam[6].Value = ddlhospital.SelectedItem.Value;

            if (ddlInsurance.Visible)
                sqlParam[7].Value = ddlInsurance.SelectedItem.Value;
            //DataSet ds_result = mgr.ExecuteQuery("dentist_advancesearch", sqlParam, "tbl_srch");


        #endregion
            if (ddlSpeciality.SelectedIndex > 0)
            {
                string StandByDentstIds = null;
                DataSet ds_result = CustomRepository.CustumDataSetFunction("dentist_advancesearch", sqlParam, "tbl_srch");
                if (ds_result != null)
                {
                     StandByDentstIds = GetStandByDentistsId(ds_result);
                }
                Session["StandByDentistId"] = StandByDentstIds;
                
                if (StandByDentstIds.Trim().Length > 0)
                {
                    Response.Redirect("discountDentist.aspx", false);

                }
                else
                {
                    _isBlank = true;
                }
            }
            else
            {
                _isSpecialty = true;
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());

        }
    }

    #region general functions

    private string GetStandByDentistsId(DataSet ds)
    {
        string DoctorsIds = "";
        DataCalendarDataContext DCD = new DataCalendarDataContext();
        var query = DCD.DentistCalendars.Select(m => m.dentistId).Distinct();

        try
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
               
                foreach (int id in query)
                {
                    var liId = (from DataRow row in ds.Tables[0].Rows
                                where ((int)row[0]) == id
                                select row[0]).FirstOrDefault();
                    if (liId != null)
                    {
                        DoctorsIds += liId.ToString() + ",";
                    }
                }
            }

        }
        catch
        {
            DoctorsIds = "";
        }
        if (DoctorsIds.Length > 1)
        { return DoctorsIds.Remove(DoctorsIds.Length - 1); }
        else
        { return DoctorsIds; }
    }



    private string GetDentistId(DataSet ds)
    {
        string DentistIds = "";
        try
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    if (i == 0)
                    { DentistIds = ds.Tables[0].Rows[i][0].ToString(); }
                    else
                    { DentistIds += "," + ds.Tables[0].Rows[i][0].ToString(); }
                }
            }
        }
        catch
        {
            DentistIds = "";
        }
        return DentistIds;
    }


    
    #endregion


    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        string countryID = ddlCountry.SelectedValue;
        // mgr.BindList("select countrycode,countryname from tbl_country", "countryname", "countrycode", ddlCountry, "select");
        mgr.BindList("select stateid,name from tbl_states where CountryID='" + countryID + "'  order by name", "Name", "StateId", ddlState, "");
        // mgr.BindList("select stateid,name from tbl_states order by name", "Name", "StateId", ddlState, "-- Any --");
        if (ddlCountry.SelectedValue == "US")
        {
            ddlState.Items.Insert(0, new ListItem("Select a State", "0"));
        }
        else
        {
            ddlState.Items.Insert(0, new ListItem("Select a Province", "0"));
        }
    }

    public class CustomId
    {
        public string Id { get; set; }
        public CustomId()
        { }

    }

    
}
